package com.ruoyi.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dyun.tool.mybatis.domain.PageDomain;
import com.dyun.tool.ssecurity.utils.YunSecurityUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.SysLog;
import com.ruoyi.common.enums.BusinessTypeEnum;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.biz.entity.BizUser;
import com.ruoyi.project.biz.service.IBizUserService;
import com.ruoyi.project.biz.wrapper.BizUserWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.properties.IntegerProperty;
import lombok.AllArgsConstructor;
import net.dreamlu.mica.core.result.R;
import net.dreamlu.mica.core.utils.$;
import net.dreamlu.mica.core.validation.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表(BizUser) 控制层
 *
 * @author dyun
 */
@RestController
@RequestMapping("biz/user")
@Api(value = "用户表接口", tags = "用户表接口")
@AllArgsConstructor
public class BizUserController extends BaseController {

    private final IBizUserService bizUserService;

    /**
     * 分页
     */
    @PreAuthorize("@ss.hasPermi('biz:user:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页", notes = "用户表列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query", dataType = IntegerProperty.TYPE)
    })
    @ApiOperationSupport(order = 1)
    public R<IPage> list(BizUser bizUser, PageDomain pageDomain) {
        IPage<BizUser> pages = bizUserService.page(startPage(pageDomain), new LambdaQueryWrapper<BizUser>());
        return success(BizUserWrapper.build().pageVO(pages));
    }

    /**
     * 详情
     */
    @PreAuthorize("@ss.hasPermi('biz:user:query')")
    @GetMapping("/detail")
    @ApiOperation(value = "详情", notes = "用户表详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "long", paramType = "query", required = true),
    })
    @ApiOperationSupport(order = 2)
    public R<BizUser> detail(Long id) {
        return success(bizUserService.getById(id));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('biz:user:edit')")
    @SysLog(title = "用户表", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/update")
    @ApiOperation(value = "修改", notes = "修改用户表")
    @ApiOperationSupport(order = 4)
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody BizUser bizUser) {
        return status(bizUserService.updateById(bizUser));
    }

    /**
     * 修改用户信息
     */
    @SysLog(title = "用户表", businessType = BusinessTypeEnum.UPDATE)
    @PreAuthorize("@ss.hasPermi('biz:user:edit')")
    @PutMapping("/partialupdate")
    public R partialupdate(@RequestBody BizUser bizUser) {
        BizUser updateUser = new BizUser();
        updateUser.setId(bizUser.getId());
        updateUser.setStatus(bizUser.getStatus());
        String password = bizUser.getPassword();
        if ($.isNotBlank(password)) {
            updateUser.setPassword(YunSecurityUtils.getPasswordEncoder().encode(password));
        }
        return status(bizUserService.updateById(updateUser));
    }
}